package com.future.permission.result;

import com.future.permission.enums.AppHttpCodeEnum;

import java.io.Serializable;


/**
 * 封装通用的返回实体类
 *
 * @param <T>
 */

public class ResponseResult<T> implements Serializable {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    private T data;

    public ResponseResult() {
        this.msg = msg;
    }

    public ResponseResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 只返回成功的状态码code和信息msg
     *
     * @return
     */
    public static ResponseResult okResult() {
        return new ResponseResult(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
    }

    /**
     * 返回成功的状态码code、信息msg和要返回的对象数据data
     *
     * @param <T>
     * @param data
     * @return
     */
    public static <T> ResponseResult okResult(T data) {
        ResponseResult responseResult = okResult();
        if (data != null) {
            responseResult.setData(data);
        }
        return responseResult;
    }

    /**
     * 返回其他状态码和信息
     *
     * @param appHttpCodeEnum
     * @return
     */
    public static ResponseResult errorResult(AppHttpCodeEnum appHttpCodeEnum) {
        return new ResponseResult(appHttpCodeEnum.getCode(), appHttpCodeEnum.getMsg());
    }

    /**
     * 返回其他状态码和自定义信息
     *
     * @param appHttpCodeEnum
     * @param msg
     * @return
     */
    public static ResponseResult errorResult(AppHttpCodeEnum appHttpCodeEnum, String msg) {
        return new ResponseResult(appHttpCodeEnum.getCode(), msg);
    }

    /**
     * 返回其他状态码、信息和对象数据
     *
     * @param appHttpCodeEnum
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult errorResult(AppHttpCodeEnum appHttpCodeEnum, T data) {
        ResponseResult responseResult = errorResult(appHttpCodeEnum);
        if (data != null) {
            responseResult.setData(data);
        }
        return responseResult;
    }

    /**
     * 返回其他状态码、自定义信息和对象数据
     *
     * @param appHttpCodeEnum
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult errorResult(AppHttpCodeEnum appHttpCodeEnum, String msg, T data) {
        ResponseResult responseResult = errorResult(appHttpCodeEnum, msg);
        if (data != null) {
            responseResult.setData(data);
        }
        return responseResult;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}